package com.tjetc.Impl;

import com.tjetc.domain.Newborn;
import com.tjetc.domain.Vaccine;
import com.tjetc.mapper.VaccinationMapper;
import com.tjetc.mapper.VaccineMapper;
import com.tjetc.service.VaccineService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.List;

public class VaccineServiceImpl implements VaccineService {
    private VaccineMapper vaccineMapper= Db.getMapper(VaccineMapper.class);
    private Result result=new Result();
    VaccinationMapper vaccination =Db.getMapper(VaccinationMapper.class);
    @Override
    public Vaccine getVaccineById(Integer id) {
        return  vaccineMapper.selectByPrimaryKey(id);
    }

    @Override
    public Vaccine getVaccineByName(String name) {
        return vaccineMapper.getVaccineByName(name);
    }

    @Override
    public List<Vaccine> getVaccineList() {
        List<Vaccine> vaccineList = vaccineMapper.getVaccineList();
        if (vaccineList.size()==0){
            System.out.println("没有疫苗");
            return null;
        }
        return vaccineList;
    }

    @Override
    public Result addVaccine(Vaccine vaccine) {
        String name = vaccine.getName();
        if (vaccineMapper.getVaccineByName(name)!=null){
            return result.setMsg("已经含有这个疫苗").setCode(500);
        }
        int i = vaccineMapper.insertSelective(vaccine);
        if (i==1){
            return result.setMsg("添加成功").setCode(200);
        }else return result.setMsg("添加失败").setCode(500);
    }

    @Override
    public Result updateVaccine(Vaccine vaccine) {
        Integer id = vaccine.getId();
        if (vaccineMapper.selectByPrimaryKey(id)==null){
            return result.setMsg("没有发现这个疫苗").setCode(500);
        }
        int i = vaccineMapper.updateByPrimaryKeySelective(vaccine);
        if (i==1){
            return result.setMsg("修改成功").setCode(200);
        }else return result.setMsg("修改失败").setCode(500);
    }

    @Override
    public Result deleteVaccine(Integer id) {
        int i = vaccineMapper.deleteByPrimaryKey(id);
        if (i==1){
            return result.setMsg("删除成功").setCode(200);
        }
        return result.setMsg("删除失败").setCode(500);
    }
    @Override
    public List<Vaccine> getVaccineByNewBornId(int id) {//获取新生儿注射的所有的疫苗
        List<Vaccine> vaccineByNewBornId = vaccineMapper.getVaccineByNewBornId(id);
        if (vaccineByNewBornId.size()==0){
            System.out.println("没有疫苗");
            return null;
        }
        return vaccineByNewBornId;
    }
    @Override
    public List<Vaccine> getVaccineByQuantity(int i) {//获取库存小于i的所有疫苗
        List<Vaccine> vaccineByQuantity = vaccineMapper.getVaccineByQuantity(i);
        if  (vaccineByQuantity.size()==0){
            System.out.println("没有疫苗");
            return null;
        }
        return vaccineByQuantity;
    }
    @Override
    public List<Vaccine> getVaccineNotByNewBornId(int  id) {
        List<Vaccine> vaccines = vaccination.VaccinationNotByNewbornId(id);//没有接种的疫苗
        return vaccines;
    }

    @Override
    public List<Vaccine> list() {
        return vaccineMapper.list();
    }
    @Override
    public List<Newborn> getlistAll() {
        List<Newborn> listAll = vaccineMapper.getListAll();
        if (listAll.size()==0){
            System.out.println("没有接种疫苗");
            return null;
        }
        return listAll;
    }
}
